<!-- 上传列表 -->
<template>
  <div class="list-info">
    <p>共计 {{ fileTaskStore.getTotal || 0 }}</p>
    <div class="failed-wrap" :class="[fileTaskStore.taskListOptions.showUploadFailed && 'active']" @click="handleChangeListStatus">
      失败 {{ fileTaskStore.getFailedTotal || 0 }}
    </div>
  </div>
  <ul v-show="!fileTaskStore.taskListOptions.showUploadFailed" class="content-list">
    <li v-for="item in fileTaskStore.getUploadList" :key="item.id" class="content-list-item">
      <div class="content-list-item-content">
        <div class="content-info">
          <div class="title-wrap">
            <div class="title">
              <i v-if="item.fileType === 'directory'" class="iconfont icon-folder-3-line"></i>
              <i v-else class="iconfont icon-file-line"></i>
              <p :title="item.name">{{ item.name }}</p>
            </div>
            <p :title="item.address">{{ item.address }}</p>
          </div>
          <div class="status">
            <i v-if="item.status === 'successful'" class="iconfont icon-checkbox-circle-fill success"></i>
            <p v-if="item.status === 'failed'">{{ item?.uploadTaskDetail?.failedCount }}失败！</p>
          </div>
          <div class="hover-status">
            <div v-if="item.status === 'failed'" class="detail-btn" @click="handleToFailedList(item)">失败详情</div>
            <a-button type="text" class="icon-only-btn" @click="fileTaskStore.removeFile(item)">
              <i class="iconfont icon-close-line"></i>
            </a-button>
          </div>
        </div>
        <a-progress v-if="item.status === 'uploading'" class="progress" :percent="item.progress" size="small" />
      </div>
    </li>
  </ul>
  <ul v-show="fileTaskStore.taskListOptions.showUploadFailed" class="content-list">
    <template v-for="item in fileTaskStore.getFailedList" :key="item.id">
      <!-- <recycle-scroller
        class="content-list"
        ref="imgListRef"
        :items="fileTaskStore.getFailedList"
        :item-size="71"
        key-field="id"
        v-slot="{ item }"
      > -->
      <li v-if="item.fileType === 'file'" class="content-list-item" :class="[`item-${item.id}`]">
        <div class="content-list-item-file" :class="[fileInfo?.id === item.id && 'active']">
          <i class="iconfont icon-file-line"></i>
          <p :title="item.name">{{ item.name }}</p>
          <a-button type="text" class="icon-only-btn" @click="fileTaskStore.removeFile(item)">
            <i class="iconfont icon-close-line"></i>
          </a-button>
        </div>
      </li>
      <li v-else class="content-list-item" :class="[`item-${item.id}`]">
        <div class="collapse">
          <div class="collapse-header" :class="[fileInfo?.id === item.id && 'active']">
            <div class="icon-caret-down-wrap" :class="[!item.expand && 'rotate']" @click="onCollapseChange(item)">
              <i class="iconfont icon-caret-down"></i>
            </div>
            <i class="iconfont icon-folder-3-line"></i>
            <p :title="item.name">{{ item.name }}</p>
            <div>（{{ item.fileList?.length }}）</div>
            <a-button type="text" class="icon-only-btn" @click="fileTaskStore.removeFailedDirectory(item)">
              <i class="iconfont icon-close-line"></i>
            </a-button>
          </div>
          <template v-if="item.expand">
            <div v-for="item2 in item.fileList" :key="item2.id" class="content-list-item-file">
              <i class="iconfont icon-file-line"></i>
              <p :title="item2.name">{{ item2.name }}</p>
              <a-button type="text" class="icon-only-btn" @click="fileTaskStore.removeFile(item2)">
                <i class="iconfont icon-close-line"></i>
              </a-button>
            </div>
          </template>
        </div>
      </li>
    </template>
    <!-- </recycle-scroller> -->
  </ul>
  <div class="clear-all" @click="handleClearAll"><i class="iconfont icon-delete-bin-fill"></i>清空</div>
</template>

<script setup lang="ts">
  import { nextTick, ref } from 'vue'

  import { FileInfo, useFileTaskStore } from './upload-store'

  const fileTaskStore = useFileTaskStore()

  const handleChangeListStatus = () => {
    fileTaskStore.changeTaskListOptions({ open: true, showUploadFailed: !fileTaskStore.taskListOptions.showUploadFailed })
    fileInfo.value = undefined
  }

  let fileInfo = ref<FileInfo>()
  const handleToFailedList = (item: FileInfo) => {
    fileInfo.value = item
    fileTaskStore.changeTaskListOptions({ open: true, showUploadFailed: true })
    fileTaskStore.updateUploadListItem(item.id as string, { expand: true })
    nextTick(() => {
      const dom = document.querySelector(`.item-${item.id}`)
      dom && dom.scrollIntoView({ block: 'start' })
    })
  }

  const onCollapseChange = (item: FileInfo) => {
    fileTaskStore.updateUploadListItem(item.id as string, { expand: !item.expand })
  }

  const handleClearAll = () => {
    fileTaskStore.taskListOptions.showUploadFailed ? fileTaskStore.removeAllFailedFile() : fileTaskStore.removeAllFile()
  }
</script>

<style lang="less" scoped>
  @import './style/common.less';
  @import '@/styles/mixins.less';

  .content-list {
    display: flex;
    overflow: auto;
    padding: 24px 24px 0;
    width: 100%;
    flex: 1;
    border-top: 1px solid @border1;
    flex-direction: column;

    .content-list-item {
      width: 100%;
      border-bottom: 1px solid @border1;

      &:last-child {
        border-bottom: none;
      }

      .content-list-item-content {
        padding: 4px 8px;
        margin: 10px 0;
        width: 100%;
        background: #fff;
        cursor: pointer;

        &:hover {
          background: rgba(0, 0, 0, 0.05);
          border-radius: 2px;

          .content-info .status {
            display: none;
          }

          .content-info .hover-status {
            display: flex;
          }
        }

        .content-info {
          display: flex;
          justify-content: space-between;
          align-items: center;
          width: 100%;

          .title-wrap {
            flex: 1;
            min-width: 0;

            .title {
              display: flex;
              align-items: center;
              width: 100%;
              font-size: 14px;
              color: @text01;
              line-height: 22px;

              > i {
                display: block;
                margin-right: 4px;
                color: @text04;
              }

              > p {
                .elliptical-1();
              }
            }

            > p {
              font-size: 12px;
              font-weight: 400;
              line-height: 20px;
              color: @text03;
              .elliptical-1();
            }
          }

          .status {
            text-align: end;

            .success {
              color: @success;
            }

            > p {
              font-size: 12px;
              font-weight: 400;
              line-height: 20px;
              color: @danger;
            }
          }

          .hover-status {
            display: none;
            gap: 0 4px;

            .detail-btn {
              display: flex;
              justify-content: center;
              align-items: center;
              width: 60px;
              height: 24px;
              font-size: 12px;
              background: rgba(0, 0, 0, 0.05);
              border-radius: 6px;
              font-weight: 400;
              line-height: 20px;
            }
          }
        }

        .progress {
          margin: 0;

          :deep(.ant-progress-bg) {
            height: 3px !important;
          }
        }
      }

      .active {
        background: rgba(0, 0, 0, 0.05);
        border-radius: 2px;
      }

      .content-list-item-file {
        display: flex;
        align-items: center;
        padding: 0 8px 0 24px;
        margin: 14px 0;
        height: 24px;
        gap: 0 4px;

        > p {
          font-size: 14px;
          font-weight: 400;
          line-height: 22px;
          .elliptical-1();
        }

        > i {
          display: block;
          color: @text04;
        }

        &:hover {
          background: rgba(0, 0, 0, 0.05);
          border-radius: 2px;

          .icon-only-btn {
            display: flex;
          }
        }

        .icon-only-btn {
          display: none;
          margin-left: auto;
        }
      }

      .collapse {
        padding: 14px 0;
      }

      .collapse-header {
        display: flex;
        align-items: center;
        padding-right: 8px;
        height: 24px;
        cursor: pointer;

        > p {
          .elliptical-1();
        }

        > i {
          display: block;
					margin-right: 4px;
          color: @text04;
        }

        &:hover {
          background: rgba(0, 0, 0, 0.05);
          border-radius: 2px;

          .icon-only-btn {
            display: flex;
          }
        }

        .icon-caret-down-wrap {
          width: 24px;
          height: 24px;
          line-height: 24px;
          text-align: center;
          flex-shrink: 0;

          > i {
            font-size: 12px;
          }

          &.rotate {
            transform: rotate(-90deg);
          }
        }

        .icon-only-btn {
          display: none;
          margin-left: auto;
        }
      }
    }
  }
</style>
